/**
 * File: PrintWriteChannelFactory.java
 * Created by: mhaimel
 * Created on: 10 Aug 2009
 * CVS:  $Id: BufferedReaderChannelFactory.java,v 1.2 2009/10/09 15:16:53 mhaimel Exp $
 */
package uk.ac.ebi.curtain.util.file.impl;

import org.apache.commons.lang.NotImplementedException;

import uk.ac.ebi.curtain.util.file.impl.FileAccessPool.ChannelType;
import uk.ac.ebi.curtain.utils.io.impl.FileIO;

/**
 * @author mhaimel
 *
 */
public class BufferedReaderChannelFactory extends AbstractFactory {

	private static final ChannelType BUFFERED_READER = ChannelType.BufferedReader;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ChannelType getType() {
		return BUFFERED_READER;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void _doActivateObject(FilePoolInfo info, ChannelWrapper cin)throws Exception {
		if(!cin.hasChannel()){
			_openChannel(info, cin);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void _doPassivateObject(FilePoolInfo info, ChannelWrapper cin)
			throws Exception {
		_closeChannel(info,cin);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void _openChannel(FilePoolInfo info, ChannelWrapper wrapper) throws Exception {
		if(!info.getRead()){
			throw new NotImplementedException("Not supported!!! " + info);
		} 
		wrapper.setChannel(
				new FileIO(info.getFile(),true)
					.getBufferedReader()
				);
	}
}
